package com.shiningstars.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

import com.shiningstars.R;
import com.shiningstars.database.DatabaseHandler;
import com.shiningstars.general.Config;
import com.shiningstars.general.Constants;
import com.shiningstars.general.Messages;
import com.shiningstars.general.Utilities;
import com.shiningstars.tools.ExceptionHandler;
import com.shiningstars.valuebeans.Feedback;

public class ViewFeedback5Activity extends Activity implements Constants, Messages, Config {

	Feedback feedback;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_teacher_feedback_5);

			Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
			SplashPageActivity.showSplash = false;

			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

			EditText editTextWorksheets = (EditText) findViewById(R.id.tf5EditText1);
			EditText editTextPrabhavna = (EditText) findViewById(R.id.tf5EditText2);
			EditText editTextPrabhavnaCost = (EditText) findViewById(R.id.tf5EditText3);
			RatingBar ratingBarPrabhavnaResponse = (RatingBar) findViewById(R.id.tf5RatingBar1);
			EditText editTextPrabhavnaComments = (EditText) findViewById(R.id.tf5EditText4);
			EditText editTextParentsComplaints = (EditText) findViewById(R.id.tf5EditText5);
			EditText editTextSanghComplaints = (EditText) findViewById(R.id.tf5EditText6);

			ratingBarPrabhavnaResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					TextView textViewPrabhavnaResponse = (TextView) findViewById(R.id.tf5TextView1);
					Utilities.setRatingDescription(textViewPrabhavnaResponse, rating);
				}
			});

			if (null != getIntent()) {
				if (null != getIntent().getExtras()) {
					if (null != getIntent().getExtras().get(FEEDBACK)) {
						feedback = (Feedback) getIntent().getExtras().get(FEEDBACK);

						editTextWorksheets.setText(feedback.getQuestion46());
						editTextPrabhavna.setText(feedback.getQuestion47());
						editTextPrabhavnaCost.setText(feedback.getQuestion48());

						if (!Utilities.isBlankString(feedback.getQuestion49())) {
							float rating = Float.parseFloat(feedback.getQuestion49());
							ratingBarPrabhavnaResponse.setRating(rating);
						}

						editTextPrabhavnaComments.setText(feedback.getQuestion50());
						editTextParentsComplaints.setText(feedback.getQuestion51());
						editTextSanghComplaints.setText(feedback.getQuestion52());

						editTextWorksheets.setEnabled(false);
						editTextPrabhavna.setEnabled(false);
						editTextPrabhavnaCost.setEnabled(false);
						ratingBarPrabhavnaResponse.setEnabled(false);
						editTextPrabhavnaComments.setEnabled(false);
						editTextParentsComplaints.setEnabled(false);
						editTextSanghComplaints.setEnabled(false);

						String currentRole = Utilities.getSharedPreference(getApplicationContext(), CURRENT_ROLE);
						if (ADMIN.equals(currentRole)) {
							DatabaseHandler databaseHandler = DatabaseHandler.getInstance(getApplicationContext());
							databaseHandler.updateFeedbackIsRead(feedback.getId());
						}
					}
				}
			}
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), ViewFeedback5Activity.class.getName(), e);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		try {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.feedback_menu, menu);
			menu.getItem(1).setTitle(CANCEL);
			menu.getItem(1).setIcon(R.drawable.ic_action_cancel);
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), ViewFeedback5Activity.class.getName(), e);
		}

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		try {
			if (BACK.equals(item.getTitle())) {
				Intent i = new Intent(ViewFeedback5Activity.this, ViewFeedback4Activity.class);
				i.putExtra(FEEDBACK, feedback);
				startActivity(i);
				finish();
			} else if (CANCEL.equals(item.getTitle())) {
				onBackPressed();
			}
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), ViewFeedback5Activity.class.getName(), e);
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		try {
			AlertDialog.Builder builder = new AlertDialog.Builder(ViewFeedback5Activity.this);

			builder.setMessage(FINISHED_VIEW_FEEDBACK);
			builder.setCancelable(false);
			builder.setPositiveButton(OK, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Intent i = null;

					String currentRole = Utilities.getSharedPreference(getApplicationContext(), CURRENT_ROLE);
					if (ADMIN.equals(currentRole)) {
						i = new Intent(ViewFeedback5Activity.this, AdminActivity.class);
					} else if (TEACHER.equals(currentRole)) {
						i = new Intent(ViewFeedback5Activity.this, MyFeedbackActivity.class);
					}

					startActivity(i);
					finish();
				}
			});
			builder.setNegativeButton(CANCEL, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {

				}
			});

			AlertDialog alert = builder.create();
			alert.show();
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), ViewFeedback5Activity.class.getName(), e);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		SplashPageActivity.showSplash = true;
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (SplashPageActivity.showSplash) {
			LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
			View popupView = layoutInflater.inflate(R.layout.activity_splash_page, null);
			final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT, true);

			findViewById(R.id.tf5TextView1).post(new Runnable() {
				public void run() {
					popupWindow.showAtLocation(findViewById(R.id.tf5TextView1), Gravity.CENTER, 0, 0);
				}
			});

			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					if (null != popupWindow && popupWindow.isShowing()) {
						popupWindow.dismiss();
					}
				}
			}, SPLASH_TIME_OUT);

			SplashPageActivity.showSplash = false;
		}
	}
}